from flask import Flask, request
import time
import os

app = Flask(__name__)


@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'


# ---------- 在线编程 ---------------
@app.route("/online_code", methods=["POST"])
def online_code():
    code = request.values.get("code")
    print(request.values)
    file_name = f"static/online_code/{int(time.time())}.py"
    with open(file_name, "w") as file:
        file.write(code)
    result = os.popen(f"python3 {file_name}").read()
    return result


if __name__ == '__main__':
    app.run()
